package cn.sm1234.test;

import cn.sm1234.Application;
import cn.sm1234.dao.EmpRepository;
import cn.sm1234.domain.Dept;
import cn.sm1234.domain.Emp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)  //让spring与junit整合
@SpringBootTest(classes = {Application.class})   // 该类是一个springboot测试类，加载启动基类
public class OneToManytest {

    @Resource
    private EmpRepository empRepository;

    @Test
    public void testSave(){
        Dept dept = new Dept();
        dept.setDeptName("HR");

        Emp emp = new Emp();
        emp.setName("Jacky");

        // 关联
        dept.getEmps().add(emp);
        emp.setDept(dept);

        //保存
        empRepository.save(emp);

    }

    @Test
    public void testFind(){
        Emp emp = empRepository.findOne(19);
        Dept dept = emp.getDept();
        System.out.println("员工：" + emp.getName() + " : 部门：" + dept.getDeptName());;
    }

}
